import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/toPromise';

/**
 * 
 * 
 * @export
 * @class TableServices
 */
@Injectable()

export class TableServices {

    /**
     * 
     * 
     * @private
     * 
     * @memberOf TableServices
     */
    private headers = new Headers({ 'Content-Type': 'application/json' });

    /**
     * Creates an instance of TableServices.
     * 
     * @param {Http} http
     * 
     * @memberOf TableServices
     */
    constructor(private http: Http) { }

    /**
     * 
     * 
     * @private
     * 
     * @memberOf TableServices
     */
    private _url = 'app/tables';

    /**
     * 
     * 
     * @returns
     * 
     * @memberOf TableServices
     */
    getTableData() {
        return this.http.get(this._url)
            .toPromise()
            .then(response => response.json().data)
            .catch(this.handleError);
    }
    getCarsSmall(url:string) {
        return this.http.get(url)
            .toPromise()
            .then(res=>res.json().data as any[])
            .then(data=>{return data});
    }

    /**
     * 
     * 
     * @private
     * @param {*} error
     * @returns {Promise<any>}
     * 
     * @memberOf TableServices
     */
    private handleError(error: any): Promise<any> {
        console.log('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }

}